<html>
	<head>
		<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
		<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
		<script src='/_ah/channel/jsapi'></script>
		<script>
			id =  Math.floor(Math.random()*100);
	        sendMessage = function(path, opt_param) {
				$.post(path, opt_param)
			 	.fail(function(){
					$("#error").html("Erreur: path="+path+" action="+action+" data="+opt_param);
			  	});
	        }

	        onOpened = function() {
	          //sendMessage('/opened');
	        	$("#token").html("Canal opened!!!");
				$.post("onlineCheckers", {action:"connectedUserList", "userId":id})
				.fail(function(){
					$("#error").html("Erreur d'initialisation");
				});        	
	        };

	        onMessage = function(m) {
	          $("#message").html(m.data);
	        }

	        openChannel = function(token) {
	          var channel = new goog.appengine.Channel(token);
	          var handler = {
	            'onopen': onOpened,
	            'onmessage': onMessage,
	            'onerror': function() {},
	            'onclose': function() {}
	          };
	          var socket = channel.open(handler);
	          socket.onopen = onOpened;
	          socket.onmessage = onMessage;
	        }

			function newToken(){
				$.post("onlineCheckers", {action:"newToken", "userId":id})
					.done(function(data) {
						openChannel(data);
						//$("#token").html(data);
					})
					.fail(function(){
						$("#token").html("Erreur d'initialisation");
					})
					.always(function(){
					
					});			
			}
		</script>
	</head>
	<body>
		<input type="button" value="new token" onclick="newToken();"/>
		<p id="token"></p>
		<p id="message"></p>
		<p id="error"></p>
	</body>
</html>